package com.campus.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.app.service.StudentsService;
import com.campus.commons.entity.OnDuty;
import com.campus.app.mapper.OnDutyMapper;
import com.campus.app.service.OnDutysService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.commons.entity.Student;
import com.campus.commons.utils.Error;
import com.campus.commons.utils.Result;
import com.campus.commons.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.Queue;

/**
 * <p>
 * 晚自习值班表 服务实现类
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@Service
@Validated
public class OnDutyServiceImpl extends ServiceImpl<OnDutyMapper, OnDuty> implements OnDutysService {
    @Autowired
    private  OnDutyMapper onDutyMapper;
    @Autowired
    private StudentsService studentsService;

    @Override
    public Result<List<OnDuty>> getNowDuty() {
        LambdaQueryWrapper<OnDuty> queryWrapper = new QueryWrapper<OnDuty>().lambda();
        queryWrapper.eq(OnDuty::getDate, Tool.getDateToString(null));
        return Result.result(onDutyMapper.selectList(queryWrapper));
    }

    @Override
    public Result<List<OnDuty>> getAllDuty( int current, int size) {
        return Result.result(onDutyMapper.selectPage(new Page<>(current,size),null));
    }

    @Override
    public Result<List<OnDuty>> getDutys( int current, int size, Date start, Date end) {
        String starts = Tool.getDateToString(start);
        String ends = Tool.getDateToString(end);
        QueryWrapper<OnDuty> queryWrapper = new QueryWrapper<>();
        queryWrapper.between(start!=null,"Date(date)", starts, ends);
        return Result.result(onDutyMapper.selectPage(new Page<>(current, size), queryWrapper));
    }

    @Override
    public Result addDuty(@NotNull OnDuty onDuty) {
//        验证参数

        return Result.result(onDutyMapper.insert(onDuty));
    }

    @Override
    public Result delDuty( Integer dutyId) {
        return Result.result(onDutyMapper.deleteById(dutyId));
    }

    @Override
    public Result upDutyVerify(@NotNull OnDuty duty) {
        //获取最新数据
        FunctionServerImpl.init();


//        验证 验证名字
        Integer studentId = studentsService.existStudentByNameFromDepend(duty.getStudentName1());
        Integer studentId2 = studentsService.existStudentByNameFromDepend(duty.getStudentName2());
        String msg= studentId == null||studentId2==null ? "没有该学生的资料" : "";
        if (msg.trim().length() != 0) {
            return Result.error(Error.Operation_E1R, msg);
        } else {
          duty.setStudentid1(studentId);
          duty.setStudentid2(studentId2);
        }
        return upDuty(duty);
    }

    @Override
    public Result upDuty( OnDuty duty) {
//        验证参数

        if (duty.getId()==null||duty.getId() <= 0) {
            return Result.error(Error.PARAMS_E3R, "参数Id不正确");
        }
        return Result.result(onDutyMapper.updateById(duty));
    }
}
